﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SilverPOP.Data.Process.Entity;

namespace SilverPOP.Data.Process.Helpers.XmlConverter
{
    public class XmlConverterHelpers
    {

        public XmlConverterHelpers() { }


      public static string GetConvertedXmlFile(List<DatabaseSchemaEntity> list,bool isWithContactList) 
       {
          
           StringBuilder str = new StringBuilder();

              str.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
              str.AppendLine("<LIST_IMPORT>");
              str.AppendLine("<LIST_INFO>");
              str.AppendLine("<ACTION>###Action###</ACTION>");
              str.AppendLine("<LIST_ID>###ListID###</LIST_ID>");
              str.AppendLine("<LIST_VISIBILITY>###ListVisibility###</LIST_VISIBILITY>");
              str.AppendLine("<FILE_TYPE>###FileType###</FILE_TYPE>");
              
              str.AppendLine("<HASHEADERS>true</HASHEADERS>");
              str.AppendLine(" </LIST_INFO>");

           str.AppendLine("<COLUMNS>");
           for (int i = 0; i < list.OrderBy(x=>x.KeyIndex).ToList().Count; i++)
           {
               str.AppendLine("<COLUMN>");
               str.AppendLine("<NAME>" + Convert.ToString(list[i].FieldName) + "</NAME>");
               str.AppendLine("<TYPE>" + Convert.ToString(list[i].FieldType) + "</TYPE>");
               str.AppendLine("<IS_REQUIRED>" + (Convert.ToBoolean(list[i].IsRequired) == true ? "true" : "false") + "</IS_REQUIRED>");
               str.AppendLine("<KEY_COLUMN>" + (Convert.ToBoolean(list[i].IsKeyColumn) == true ? "true" : "false") + "</KEY_COLUMN>");
               str.AppendLine("<DEFAULT_VALUE/>");
               str.AppendLine("</COLUMN>");
           }
           str.AppendLine("</COLUMNS>");

           str.AppendLine("<MAPPING>");
           for (int i = 0; i < list.OrderBy(x => x.KeyIndex).ToList().Count; i++)
           {
               str.AppendLine("<COLUMN>");
               str.AppendLine("<INDEX>" + Convert.ToString(list[i].KeyIndex) + "</INDEX>");
               str.AppendLine("<NAME>" + Convert.ToString(list[i].FieldName) + "</NAME>");
               str.AppendLine("<INCLUDE>" + (Convert.ToBoolean(list[i].IsInclude) == true ? "true" : "false") + "</INCLUDE>");
               str.AppendLine("</COLUMN>");
           }
           str.AppendLine("</MAPPING>");
           if (isWithContactList == true)
               str.AppendLine("###ContactList###");
           str.AppendLine("</LIST_IMPORT>");


           return str.ToString();
       }


    }
}
